dump("// $Id: testConvertPageEurozone.js,v 1.5 2007/02/28 09:14:57 Will Exp $;\n");

var WILLTEST = top.WILLTEST;
var assertCausesException  = WILLTEST.assertCausesException;
var assertFailCreateObject = WILLTEST.assertFailCreateObject;
var assertCreateObject     = WILLTEST.assertCreateObject;
var assertValidRef         = WILLTEST.assertValidRef;
var assertArraysEqual      = WILLTEST.assertArraysEqual;
var traceTest              = WILLTEST.traceTest;
var newListFromCodes       = WILLTEST.newListFromCodes;

//Use the name of the tests you want to run, for example "testExchangeRateCache,testNumberSwissQuote";
// (if it's already set in another file then it won't be overridden)
traceTest.limitTo = traceTest.limitTo || ""; 

var VMC      = WILLTEST.VMC;

if (!WILLTEST.viewMyCurrency) { alert('These tests must be run with VMC active'); }

var gViewMyCurrency = WILLTEST.viewMyCurrency;
var gPrefService    = gViewMyCurrency.prefManager._ps; // the low-level prefService

var gCodesToTest=['EUR', // all the old eurozone rates depend on this
		  'USD', // to test conversion to another rate
		  'ATS', 'BEF', 'FIM', 'FRF', 'DEM', 'GRD', 'NLG', 'IEP', 'ITL', 'LUF', 'PTE', 'ESP', 'SIT']; // 'VAL' conflicts with ITL

var gCurrencyList = newListFromCodes(gCodesToTest);
var USD = gCurrencyList.findFromCode('USD');
var EUR = gCurrencyList.findFromCode('EUR');



function checkTabelCell(comment,id) {
  var cellContents = document.getElementById(id).textContent; //1936.27 XXX = 1936.27 ITL //WILL: not using VMC.ge because its not this window
  //vmc.hack('Table Cell '+id+' contents:\t'+cellContents);
  var r=/(\S+).+?=\s*(\S+)/.exec(cellContents);
  var converted = Number(r[1]).valueOf();
  var original  = Number(r[2]).valueOf();
  assertEquals(comment+' values are equal for '+id,converted,original);
  assert(comment+' converted > 0 for '+id,(converted > 0));
  assert(comment+' original  > 0 for '+id,(original  > 0));
}

function setTargetCurrency(currency) {
  gViewMyCurrency._setTargetCurrency(currency);
  WILLTEST.sleep(500); 
}


function testRestoreSettings() {   //WILL: we rely on this test running last. (it's first in the file)
  gPrefService.restorePrefs(); //just for info, this triggers the pref observers
  VMC.Number.performRounding = this.oldPerformRounding;
}

function testConversionOfAllIntoAll() {
  if (!traceTest('Check that all the currencies can be converted into all the old currencies. NB two levels deep')) { return; }
  var convertAndCheck = function(toCode) {
    var c = gCurrencyList.findFromCode(toCode);
    setTargetCurrency(c);
    VMC.applyFuncToArray(function(fromCode) { checkTabelCell('from='+fromCode+' to='+toCode,fromCode); } ,gCodesToTest);  
  };

  VMC.applyFuncToArray( convertAndCheck, gCodesToTest);
  gViewMyCurrency.disableConversion();
}

function testConversionOfOldIntoEuro() {
  if (!traceTest('Check that the old eurozone currencies all convert into one euro')) { return; }
  setTargetCurrency(EUR);
  VMC.applyFuncToArray(function(code) { checkTabelCell('1 EUR = original',code); } ,gCodesToTest);
  gViewMyCurrency.disableConversion();
}

function testConversionOfOldIntoUsd() {
  if (!traceTest('Check that the old eurozone currencies converting into one EUR in USD')) { return; }
  setTargetCurrency(USD);

  VMC.applyFuncToArray(function(code) { checkTabelCell('one EUR in USD = original in USD',code); } ,gCodesToTest);
  gViewMyCurrency.disableConversion();
}



/////////////////////////////////////////////////
// this MUST BE LAST so that is executed first //
/////////////////////////////////////////////////

function testSetup() { 
  gPrefService.storePrefs();
  var pm = gViewMyCurrency.prefManager;
  pm.setSourceCurrencyList(gCurrencyList);
  pm.setTargetCurrencyList(gCurrencyList);
  pm.setHideOriginalCurrency(true); 
  pm.setDecimalSeparator('.');
  pm.setThousandsSeparator('');
  
  WILLTEST.sleep(2000);  // give the exchange rate cache time to update
  
  var EUR_USD = 1.1954; // this is the fixed rate in the test HTML
  var USD_EUR = 1/EUR_USD;
  var r1 = gViewMyCurrency.exchangeRateCache.getRateObj(EUR,USD);
  var r2 = gViewMyCurrency.exchangeRateCache.getRateObj(USD,EUR);
  r1.setRate(EUR_USD); pm.writeRate(r1);
  r2.setRate(USD_EUR); pm.writeRate(r2);
  
  WILLTEST.sleep(1000);  // give the exchange rate cache time to update all the dependant rates

  // we need to turn off rounding, because the HTML table contains values with lots of decimal places
  // and we want to compare those exactly
  this.oldPerformRounding = VMC.Number.prototype.performRounding;
  VMC.Number.prototype.performRounding = false;
  
}
